package com.iscc.maoke.dao.impl;

import com.iscc.maoke.dao.ProductDAO;
import com.iscc.maoke.domain.Product;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

public class ProductDAOImpl implements ProductDAO {
    @Override
    public int add(Connection conn, Product product) {
        int result = -1;
        String sql = "INSERT INTO tab_product(pno,pname,tno,price,discount,cover,summary) VALUES(?,?,?,?,?,?,?)";

        try {
            PreparedStatement psmt = conn.prepareStatement(sql);
            psmt.setInt(1, product.getPno());
            psmt.setString(2, product.getPname());
            psmt.setInt(3, product.getTno());
            psmt.setDouble(4, product.getPrice());
            psmt.setDouble(5, product.getDiscount());
            psmt.setString(6, product.getCover());
            psmt.setString(7, product.getSummary());
            result = psmt.executeUpdate();

        } catch (SQLException e) {
            e.printStackTrace();
        }
        return result;
    }

    @Override
    public int getNewId(Connection conn) {
        int id = 0;
        String sql = "SELECT MAX(pno) FROM tab_product";
        try {
            PreparedStatement psmt = conn.prepareStatement(sql);
            ResultSet rs = psmt.executeQuery();
            if (rs.next()) {
                if (String.valueOf(rs.getInt(1)) != null) {
                    id = rs.getInt(1);
                }
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }

        return id + 1;
    }

    @Override
    public int add(Object o) {
        return 0;
    }

    @Override
    public int update(Object o) {
        return 0;
    }

    @Override
    public int del(int id) {
        return 0;
    }

    @Override
    public List queryAll() {
        return null;
    }

    @Override
    public Object queryById(int id) {
        return null;
    }
}
